<?php
    class DatatypesView
    {
        private $controller;
        
        // Set a reference to the controller
        public function setController($controller)
        {
            $this->controller = $controller;
        }
        
        // Show top
        public function showTop()
        {
            echo '
                <h1>Datatypes</h1>
                <p>Manage your datatypes here.</p>
                <form method="post" action="index.php?datatypes">
            ';
        }
        
        // Show bottom
        public function showBottom()
        {
            echo '
                </form>
            ';
        }
        
        // Show add/edit form
        public function showForm($values) {
            echo '
                <div>
                    <label>Name: </label>	
                    <input type="text" name="name" class="text" value="'.$values['name'].'" />
                </div>
                <div>
                    <label>Description:</label>
                    <input type="text" name="description" class="text" value="'.$values['description'].'" />
                </div>
                <h3>Options</h3>
                <div class="options">
                    <ol>
            ';
            $nr = 1;
            $included = true;
            if($values['action']=='add') {                
                include_once('core/ajax.datatypes_option.php'); 
            } else {
                $options = $this->controller->getOptions($values['id']);                
                foreach($options as $option) {
                    $id_option = $option['id_option'];
                    $hasmany   = $option['hasmany'];                    
                    include('core/ajax.datatypes_option.php');							
                    $nr++;							
                }
            }
            echo '
                    </ol>
                    <var id="nr">'.$nr.'</var>                    
                </div>
                <div>
                    <label>&nbsp;</label><a href="#" class="add">Add extra option</a>
                </div>
                <label>&nbsp;</label>
                <input type="hidden" name="action" value="'.$values['action'].'" />
                <input type="hidden" name="id" value="'.$values['id'].'" />
                <input type="submit" value="Save" name="save" />
            ';
        }
        
        // Show no records available:
        public function showNoRecordsAvailable() {
            echo '
                <p><em>There are no datatypes available...</em></p>
            ';
        }
        
        // Show the records:
        public function showRecords($records) {
            echo '
                <table class="zebra optionlist">
                    <tr>
                        <th>&nbsp;</th>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Description</th>
                    </tr>
            ';
            foreach($records as $record) {
                echo '
                    <tr>
                        <td><input type="radio" name="id" value="'.$record['id'].'" /></td>
                        <td>'.$record['id'].'</td>
                        <td>'.$record['name'].'</td>
                        <td>'.$record['description'].'</td>
                    </tr>
                ';
            }
            echo '
                </table>
            ';
        }
        
        // Show Add/Edit/Delete buttons:
        public function showAddEditDelete() {
            echo '
                <input type="submit" name="edit" value="edit..." />
                <input type="submit" name="delete" value="delete" />
                <input type="submit" name="add" value="add new..." />            
            ';
        }        
    }
?>